package com.practice.bootdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dto.PageVo;
import com.practice.bootdemo.dto.CodeTestPageDto;
import com.practice.bootdemo.entity.CodeTest;
import com.practice.bootdemo.mapper.CodeTestMapper;
import com.practice.bootdemo.service.CodeTestService;
import com.practice.bootdemo.vo.CodeTestVo;
import com.util.BeanExtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;

/**
* @author :zhoushunquan
* @description: 代码生成服务接口实现类
* @create date: 2022-03-24 17:48:37
*/

@Slf4j
@Service
public class CodeTestServiceImpl implements CodeTestService {

    @Autowired
    CodeTestMapper codeTestMapper;

    /**
    * 保存代码生成
    * @param codeTest
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(CodeTest codeTest) {
        return codeTestMapper.insert(codeTest);
    }

    /**
    * 根据ID更新代码生成
    * @param codeTest
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(CodeTest codeTest) {
        return codeTestMapper.updateById(codeTest);
    }

    /**
    * 删除代码生成
    * @param test_id
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long test_id) {
        return codeTestMapper.deleteById(test_id);
    }

    /**
    * 根据查询代码生成
    * @param test_id
    * @return
    */
    @Override
    public CodeTestVo detail(Long test_id) {
        CodeTest codeTest= codeTestMapper.selectById(test_id);
        return BeanExtUtil.copyTo(codeTest, CodeTestVo.class);
    }

    /**
    * 查询代码生成
    * @param codeTest
    * @return
    */
    @Override
    public List<CodeTestVo> findList(CodeTest codeTest) {
        LambdaQueryWrapper<CodeTest> queryWrapper = new LambdaQueryWrapper<>(new CodeTest()).
                select(CodeTest::getName,CodeTest::getUserSign);
//        queryWrapper.setEntity(codeTest);
       /* queryWrapper.eq(StringUtils.isNotBlank(codeTest.getName()),CodeTest::getName,codeTest.getName());
        queryWrapper.like(StringUtils.isNotBlank(codeTest.getAddress()),CodeTest::getAddress,codeTest.getAddress());*/

        queryWrapper.eq(CodeTest::getName,codeTest.getName());
        queryWrapper.like(CodeTest::getAddress,codeTest.getAddress());

//        QueryWrapper<CodeTestVo> wrapper=new QueryWrapper();

        List<CodeTest> list= codeTestMapper.selectList(queryWrapper);
        return BeanExtUtil.copyList(list,CodeTestVo.class);
    }

    /**
    * 查询代码生成分页信息
    * @param codeTestPageDto
    * @return PageVO
    */


    @Override
    public PageVo findPageList(CodeTestPageDto codeTestPageDto) {
        Page<CodeTest> page=new Page<>(codeTestPageDto.getPageNum(),codeTestPageDto.getPageSize());
        CodeTest codeTest = BeanExtUtil.copyTo(codeTestPageDto, CodeTest.class);
        QueryWrapper<CodeTest> queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(codeTest);
//         page.addOrder(OrderItem.desc("id"));
//        codeTestMapper.selectPage(page, queryWrapper);
        codeTestMapper.selectPageList(page, codeTest);
        return BeanExtUtil.copyTo(page, PageVo.class);
    }


   }